package com.beetlestech.nfe.main.biz.com.impl;

import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.main.biz.com.HbAdminEdit;
import com.beetlestech.nfe.main.biz.com.dto.AdmineditDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.constant.SQLID;
import com.beetlestech.nfe.main.db.tbl.MstusrDTO;
import com.beetlestech.nfe.main.db.tbl.UserjurisdictionDTO;


public class HbAdminEditImpl extends AbstractDBLogic implements HbAdminEdit, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String SH_INFO = "用户信息表";

	//根据ID 查询user表与Userjurisdiction表数据
	public AdmineditDTO GetUserByUid(Integer usr_id) {
		//创建BIZ层DTO 对象
		AdmineditDTO ad=new AdmineditDTO();
		//创建DBDto对象
		MstusrDTO userDTO = new MstusrDTO();
		userDTO.setusrid(usr_id);
		userDTO = queryDAO.queryForObject(MstusrDTO.EQUAL_SEARCH, userDTO);
		
		
		UserjurisdictionDTO juDTO=new UserjurisdictionDTO();
		juDTO.setusrid(usr_id);
		List<UserjurisdictionDTO> listj = queryDAO.queryForObjectList(UserjurisdictionDTO.EQUAL_SEARCH, juDTO);
		
		List<UserjurisdictionDTO> listh=new ArrayList<UserjurisdictionDTO>();
		for(UserjurisdictionDTO item : listj){
			listh.add(item);
		}
		//list 转换成数组
		UserjurisdictionDTO[] arr = (UserjurisdictionDTO[])listh.toArray(new UserjurisdictionDTO[listh.size()]);
		ad.setusername(userDTO.getusername());
		ad.setPassword(userDTO.getpassword());
		ad.setJurisdictionid(arr);
		
		return ad;
		
	}
	
	//修改管理员信息
	@Override
	public void UpdateAdminBaseInfo(Integer usr_id,
															String username,
															String password,
															Integer au01,
															Integer au02,
															Integer au03,
															Integer au04,
															Integer au05,
															Integer au06
			){
		
		//向USERDTO里插入的数据
		MstusrDTO userDTO = new MstusrDTO();
			userDTO.setusrid(usr_id);
			userDTO.setusername(username);
			userDTO.setpassword(password);
		
			//对DB执行操作
			updateDAO.updateOne(MstusrDTO.UPDATE, userDTO, M10190WS, SH_INFO);
		
		//插入之前先根据ID 把JU表的数据删除
		UserjurisdictionDTO juDlDTO=new UserjurisdictionDTO();
		
			juDlDTO.setusrid(usr_id);
			updateDAO.update(SQLID.USER_JURISDICTION_DELETE_BYUID, juDlDTO);
			
			
			//想UserjurisdictionDTO里插入数据
			for(int i=1;i<7;i++){
				UserjurisdictionDTO juDTO=new UserjurisdictionDTO();
				if(au01!=null && au01==i) {
					juDTO.setusrid(usr_id);
					juDTO.setjurisdictionid(1);
					updateDAO.updateOne(UserjurisdictionDTO.INSERT, juDTO, M10190WS, SH_INFO);
				}
				if(au02!=null && au02==i){
					juDTO.setusrid(usr_id);
					juDTO.setjurisdictionid(2);
					updateDAO.updateOne(UserjurisdictionDTO.INSERT, juDTO, M10190WS, SH_INFO);
				}
				if(au03!=null && au03==i){
					juDTO.setusrid(usr_id);
					juDTO.setjurisdictionid(3);
					updateDAO.updateOne(UserjurisdictionDTO.INSERT, juDTO, M10190WS, SH_INFO);
				}
				if(au04!=null && au04==i){
					juDTO.setusrid(usr_id);
					juDTO.setjurisdictionid(4);
					updateDAO.updateOne(UserjurisdictionDTO.INSERT, juDTO, M10190WS, SH_INFO);
				}
				if(au05!=null && au05==i){
					juDTO.setusrid(usr_id);
					juDTO.setjurisdictionid(5);
					updateDAO.updateOne(UserjurisdictionDTO.INSERT, juDTO, M10190WS, SH_INFO);
				}
				if(au06!=null && au06==i){
					juDTO.setusrid(usr_id);
					juDTO.setjurisdictionid(6);
					updateDAO.updateOne(UserjurisdictionDTO.INSERT, juDTO, M10190WS, SH_INFO);
			}
		}
	}
	
	@Override
	public void SaveAdminBaseInfo(String username,String password,
															Integer au01,
															Integer au02,
															Integer au03,
															Integer au04,
															Integer au05,
															Integer au06
			){
		
		//向USERDTO里插入的数据
		MstusrDTO userDTO = new MstusrDTO();
			
			userDTO.setusername(username);
			userDTO.setpassword(password);
			userDTO.settype(1);
			userDTO.setstate(1);
			userDTO.setfirstloginflg(0);
			//对DB执行操作
			updateDAO.updateOne(MstusrDTO.INSERT, userDTO, M10190WS, SH_INFO);
		
		
			
			//想UserjurisdictionDTO里插入数据
			for(int i=1;i<7;i++){
				UserjurisdictionDTO juDTO=new UserjurisdictionDTO();
				if(au01!=null && au01==i) {
					juDTO.setusrid(userDTO.getusrid());
					juDTO.setjurisdictionid(1);
					updateDAO.updateOne(UserjurisdictionDTO.INSERT, juDTO, M10190WS, SH_INFO);
				}
				if(au02!=null && au02==i){
					juDTO.setusrid(userDTO.getusrid());
					juDTO.setjurisdictionid(2);
					updateDAO.updateOne(UserjurisdictionDTO.INSERT, juDTO, M10190WS, SH_INFO);
				}
				if(au03!=null && au03==i){
					juDTO.setusrid(userDTO.getusrid());
					juDTO.setjurisdictionid(3);
					updateDAO.updateOne(UserjurisdictionDTO.INSERT, juDTO, M10190WS, SH_INFO);
				}
				if(au04!=null && au04==i){
					juDTO.setusrid(userDTO.getusrid());
					juDTO.setjurisdictionid(4);
					updateDAO.updateOne(UserjurisdictionDTO.INSERT, juDTO, M10190WS, SH_INFO);
				}
				if(au05!=null && au05==i){
					juDTO.setusrid(userDTO.getusrid());
					juDTO.setjurisdictionid(5);
					updateDAO.updateOne(UserjurisdictionDTO.INSERT, juDTO, M10190WS, SH_INFO);
				}
				if(au06!=null && au06==i){
					juDTO.setusrid(userDTO.getusrid());
					juDTO.setjurisdictionid(6);
					updateDAO.updateOne(UserjurisdictionDTO.INSERT, juDTO, M10190WS, SH_INFO);
			}
		}
	}
	
}
